//
//  PayOffViewController.m
//  HTP
//  未支付页面
//  Created by 王鑫 on 15/11/2.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "PayOffViewController.h"
#import "PayTableViewCell.h"
#import "PayOffOrderInfoViewController.h"
#import "MyOrderViewModel.h"
#import "NSDate+FSExtension.h"

@interface PayOffViewController () <BaseTableDelgate,UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,retain) BaseTableView *payTableView;

@property (nonatomic, retain) NSArray *orderArray;

@property (nonatomic, retain) MyOrderModel *cellData;

@end

@implementation PayOffViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _orderArray = [NSArray array];
    [self.view addSubview:self.payTableView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getLocalData];
    [self getUnFinishOrder];
}

- (BaseTableView *)payTableView {
    if (!_payTableView) {
        _payTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, ScreenHeihgt - NavHe - 39) style:UITableViewStylePlain withHeadRefresh:NO andFoodRefresh:NO];
        _payTableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:0.7];
        _payTableView.delegate = self;
        _payTableView.dataSource = self;
        _payTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    
    return _payTableView;
}


//TODO:UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_orderArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *Identifier = @"Identifier";
    PayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    
    if (cell == nil) {
        cell = [[PayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    _cellData = [_orderArray objectAtIndex:indexPath.section];
    [cell setCellData:_cellData];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PayOffOrderInfoViewController *payOfforderInfoView = [[PayOffOrderInfoViewController alloc] init];
    _cellData = [_orderArray objectAtIndex:indexPath.section];
    payOfforderInfoView.orderID = [NSString stringWithFormat:@"%@", _cellData.ordId];
    [[NSNotificationCenter defaultCenter] postNotificationName:ROOTGOTONEXTCONTROLLER object:payOfforderInfoView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == _orderArray.count - 1) {
        return 0;
    }
    return 10;
}
//TODO:获取未支付订单
-(void)getLocalData{
    NSPredicate *orderFilter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@" parentId = %@ AND ordStatus = 1",[NSNumber numberWithInt:[[USERCONTENT getUserParentId] intValue]]]];
    NSArray *orderDatas = [OrderData findByPredicate:orderFilter andLimit:10000 andStart:0 BySorted:@"ordId" ascending:NO];
    NSMutableArray *orderList = [NSMutableArray array];
    [orderDatas enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //修改本地过期单子
        OrderData *orderData = (OrderData *)obj;
        NSDate *date = [NSDate fs_dateFromString:orderData.ordCreatTime format:@"yyyy-MM-dd HH:mm:ss"];
        NSInteger seconds = -(int)[date timeIntervalSinceNow];
        if (seconds >= 1800) {
            orderData.ordStatus = [NSNumber numberWithInt:5];
            [XLCoreDataManager save];
        }
        else{
            [orderList addObject:obj];
        }
    }];
    _orderArray = [[NSMutableArray alloc] initWithArray:orderList];
    [self.payTableView reloadData];
}
-(void)getUnFinishOrder{
    MyOrderViewModel *myOrderVM = [[MyOrderViewModel alloc] init];
    [myOrderVM setBlockWithReturnBlock:^(id returnValue) {
        [self hiddenMbProgress];
        _orderArray = [MyOrderModel mj_objectArrayWithKeyValuesArray:returnValue[INTERFACE_INFO]];
        [_orderArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            MyOrderModel *order = (MyOrderModel *)obj;
            [order saveBean];
        }];
        [self getLocalData];
    } WithErrorBlock:^(id errorCode) {
         [self hiddenMbProgress];
    } WithFailureBlock:^{
         [self hiddenMbProgress];
    }];
    [self showMbProgress:@"获取订单"];
    [myOrderVM getUnFinishOrderList:[USERCONTENT getUserParentId]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
